/*工具类*/
var CoreUtil = (function () {
    var coreUtil = {};

    //paraName为要获取的参数名称
    coreUtil.getUrlParam = function(paraName){
        var url = document.location.toString();
        var arrObj = url.split("?");
        if (arrObj.length > 1) {
            var arrPara = arrObj[1].split("&");
            var arr;
            for (var i = 0; i < arrPara.length; i++) {
                arr = arrPara[i].split("=");

                if (arr != null && arr[0] == paraName) {
                    return decodeURI(arr[1]);
                }
            }
            return "";
        }
        else {
            return "";
        }
    }

    /*GET*/
    coreUtil.sendGet = function(url, params, ft){
        this.sendAJAX(url, params, ft, "GET")
    }

    /*POST*/
    coreUtil.sendPost = function(url, params, ft){
        this.sendAJAX(url, JSON.stringify(params), ft, "POST")
    }
    /*PUT*/
    coreUtil.sendPut = function(url, params, ft){
        this.sendAJAX(url, JSON.stringify(params), ft, "PUT")
    }
    /*DELETE*/
    coreUtil.sendDelete = function(url, params, ft){
        this.sendAJAX(url, JSON.stringify(params), ft, "DELETE")
    }


    /*ajax*/
    coreUtil.sendAJAX = function(url, params, ft, method){
        // var loadIndex = top.layer.load(0, {shade: false});
        $.ajax({
            url: url,
            cache: false,
            async: true,
            data: params,
            type: method,
            contentType: 'application/json; charset=UTF-8',
            dataType: "json",
            beforeSend: function (request) {
                request.setRequestHeader("userTicket",sessionStorage.getItem("userTicket"));
            },
            success: function (res) {
                // top.layer.close(loadIndex);
                if (res.code==200){
                    if(ft!=null&&ft!=undefined){
                        ft(res);
                    }
                }else if(res.code==401001){ //凭证过期重新登录
                    layer.msg("凭证过期请重新登录", {time:2000}, function () {
                        top.window.location.href="/index/login"
                    })
                }else if(res.code==401008){ //无权限
                    layer.msg("抱歉！您暂无权限", {time:2000,icon: 7})
                } else {
                    layer.msg(res.msg, { icon: 7 });
                }
            },
            error:function (XMLHttpRequest, textStatus, errorThrown) {
                // top.layer.close(loadIndex);
                if(XMLHttpRequest.status==404){
                    top.window.location.href="/index/404";
                }else{
                    layer.msg("服务器好像除了点问题！请稍后试试",{icon: 7});
                }
            }
        })
    }


    /*存入本地缓存*/
    coreUtil.setData = function(key, value){
        layui.data('LocalData',{
            key :key,
            value: value
        })
    };
    /*从本地缓存拿数据*/
    coreUtil.getData = function(key){
        var localData = layui.data('LocalData');
        return localData[key];
    };

    //判断字符是否为空的方法
    coreUtil.isEmpty = function(obj){
        if(typeof obj == "undefined" || obj == null || obj == ""){
            return true;
        }else{
            return false;
        }
    }

    //字典数据回显
    coreUtil.selectDictLabel = function (datas, value) {
        datas = JSON.parse(datas);
        var label = "";
        $.each(datas, function(index, dict) {
            if (dict.value == ('' + value)) {
                label = dict.label;
                return false;
            }
        });
        //匹配不到，返回未知
        if (CoreUtil.isEmpty(label)) {
            return "未知";
        }
        return label;
    }
    coreUtil.blobDownload = function(src){
        var url = src;
        var xhr = new XMLHttpRequest();
        xhr.open("GET",url,true);//可使用post
        xhr.setRequestHeader("authorization",CoreUtil.getData("access_token"));
        xhr.responseType = "blob";//返回类型；
        //请求完成处理函数
        xhr.onload = function(){
            if(this.status === 200){
                var type = this.response.type;
                console.log(this.response.type);
                if(type === "text/html"){
                    //layer.msg("请登陆后进行下载",{icon: 7});
                    layer.msg("系统异常，请稍后再试",{icon: 7});
                    return
                }
                if(type === "application/json"){
                    layer.msg("系统异常，请稍后再试",{icon: 7});
                    return
                }
                if(type === "application/octet-stream"){
                    var blob = this.response;
                    var filename = this.getResponseHeader("filename");
                    filename = decodeURI(filename);
                    var objectUrl = window.URL.createObjectURL(blob);
                    //转换完成
                    var a = document.createElement('a');
                    a.download = filename;
                    a.href = objectUrl;
                    a.click();
                    a.remove();
                    window.URL.revokeObjectURL(objectUrl);//释放
                }else{
                    layer.msg("系统异常，请稍后再试",{icon: 7});
                }
            }
        }
        xhr.send(null)
    }


    return coreUtil;
})(CoreUtil, window);
